package com.yh.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yh.entity.SysPermission;
import com.yh.entity.SysUser;
import com.yh.entity.vo.MenuVO;
import com.yh.mapper.SysPermissionMapper;
import com.yh.mapper.SysUserMapper;
import com.yh.security.SysUserDTO;
import com.yh.service.ISysPermissionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 权限表 权限表 服务实现类
 * </p>
 *
 * @author yh
 * @since 2021-04-06
 */
@Service
@AllArgsConstructor
public class SysPermissionServiceImpl extends ServiceImpl<SysPermissionMapper, SysPermission> implements ISysPermissionService {
    private final SysPermissionMapper sysPermissionMapper;
    private final SysUserMapper sysUserMapper;

    @Override
    public SysPermission createPermission(SysPermission sysPermission) {
        sysPermissionMapper.insert(sysPermission);
        return sysPermission;
    }

    @Override
    public SysPermission updatePermission(SysPermission sysPermission) {
        sysPermissionMapper.updateById(sysPermission);
        return sysPermission;
    }

    @Override
    public SysPermission getInfo(Long id) {
        SysPermission permission = sysPermissionMapper.selectById(id);
        return permission;
    }

    @Override
    public Page<SysPermission> getPage(Integer pageNum, Integer pageSize) {
        Page<SysPermission> sysPermissionPage = sysPermissionMapper.selectPage(new Page<>(pageNum, pageSize), new LambdaQueryWrapper<>());
        return sysPermissionPage;
    }

    @Override
    public List<MenuVO> getMenus() {
        List<SysPermission> sysPermissions = sysPermissionMapper.selectList(new LambdaQueryWrapper<>());
        List<MenuVO> menuVOS = new ArrayList<>();
        MenuVO menuVO = null;
        for(SysPermission permission : sysPermissions){
            menuVO = new MenuVO();
            menuVO.setId(permission.getId());
            menuVO.setPid(permission.getPid());
            menuVO.setName(permission.getName());
            menuVO.setPath(permission.getValue());
            menuVO.setComponent(permission.getComponent());
            menuVOS.add(menuVO);
        }
        List<MenuVO> result = menuVOS.stream().filter(item -> 0==item.getPid()).map(item -> {
            item.setChildren(getChildren(item, menuVOS));
            return item;
        }).collect(Collectors.toList());
        return result;
    }

    public static List<MenuVO> getChildren(MenuVO root, List<MenuVO> all){
        List<MenuVO> children = all.stream().filter(item -> item.getPid().equals(root.getId()))
                .map((item) -> {
                    item.setChildren(getChildren(item, all));
                    return item;
                }).collect(Collectors.toList());
        return children;
    }

    public SysUserDTO userPermissions(String username){
        SysUser user = sysUserMapper.selectOne(new LambdaQueryWrapper<SysUser>().eq(SysUser::getUsername, username));
        if(StringUtils.isEmpty(user)){
            return null;
        }
        SysUserDTO sysUserDTO = new SysUserDTO();
        sysUserDTO.setUsername(user.getUsername());
        sysUserDTO.setPassword(user.getPassword());

        List<String> userPermission = sysPermissionMapper.getUserPermission(user.getId());
        if(userPermission.size() > 0){
            List<GrantedAuthority> list = new ArrayList<>();
            userPermission.forEach(item -> list.add(new GrantedAuthority() {
                @Override
                public String getAuthority() {
                    return item;
                }
            }));
            sysUserDTO.setAuthorities(list);
        }
        return sysUserDTO;
    }
}
